/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Contains a list of all the groups a user is a member of
 * User is able to go to a group's home page by clicking on the name
 * User is able to delete / search / add / create a group
 * 
 * 
 * @author Kyu
 */
public class GroupPersonalServlet extends HttpServlet {
   
    String[] Groups = {"GroupA", "GroupC"};
    String[] Descriptions = {"This is description of GroupA", "This is description of GroupC"};
    String[] totalMemArray = {"12", "14"};
    String[] creators = {"Jessy", "Carly"};
    
    
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession httpSession = request.getSession(true);
        PrintWriter out = response.getWriter();
        try {
        out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet GroupMainServlet</title>");  
            out.println("<link rel=\"stylesheet\" href=\"inbox.css\" type=\"text/css\">");
            generateInlineJS(out);
            CSSGenerator.generateNewsGroupMainPageInlineCSS(out);
       
            out.println("</head>");
            out.println("<body>");
            
            out.println(
                    "<center>" + 
                     "<table width=\"60%\" border>" + 
                    "<tr><br><input type=\"button\" value = \"View All Groups\" onClick=\"window.open(\'GroupMainServlet\')\"></br></tr>" 
                    ); 

             for (int i = 0; i < Groups.length; i++)
             {
                 out.println(
                      /* first row */
                     "    <tr>" + 
                     "     <td><p class=\"ngroupTitle\"><a href=\"GroupAServlet\">" + Groups[i] + "</a></p>" +  
                     // When group name clicked, go to the group's page                                          
                     "     </td>" +
                     "     <td><p class=\"ngroupTitle\"> " +
                     "     Description: </p>" + Descriptions[i] + // Description
                     "     </td>" +
                     "     <td><input type=\"button\" id=\"Delete_this_group\" value=\"Delete\" onClick=\"\"> " +  // Delete Button
                     // TODO: delete button action
                     // Go to DB, remove the group from the person's group table
                     // Refresh the page without the deleted group
                     
                     "     </td>" +
                     "    </tr>" + 
                     
                     /* second row */
                     "   <tr>" + 
                     "      <td>" +
                     "      </td>" + 
                     "      <td><p class=\"ngroupTitle\"> " +
                     "      Total Members: </p>" + totalMemArray[i] + // total members
                     "      </td>" +
                     "      <td>" + 
                     "      </td>" +
                     "    </tr>" + 
                     
                     /* third row */
                     "    <tr>" + 
                     "      <td>" +
                     "      </td>" + 
                     "      <td><p class=\"ngroupTitle\"> " +
                     "      Creator: </p>" + creators[i] + // creator
                     "      </td>" +
                     "      <td>" + 
                     "      </td>" +
                     "    </tr>"
                     );
           }
            out.println( "</Table>");
            out.println("</body>");
            out.println("</html>");
            
          
        } finally { 
            out.close();
        }
    } 
    
  private void generateInlineJS(PrintWriter out)
    {
        out.println(
                 "<script language=\"javascript\"> " +
         "function loadwindow(target)" +
         "{" +
         "       window.open(target,\"\",\"width=800,height=600,status=1,menubar=0,resizable=1\");" +      
         "}" +
         "</script>"
       );
    }
  
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
